package com.ettractions.kiosk.services
{
	import com.ettractions.kiosk.events.KioskDataServiceEvent;
	import com.ettractions.kiosk.vo.AdVO;
	import com.ettractions.kiosk.vo.BrochureMediaVO;
	import com.ettractions.kiosk.vo.BrochureVO;
	import com.ettractions.kiosk.vo.CategoryVO;
	import com.ettractions.kiosk.vo.ContactInformationVO;
	import com.ettractions.kiosk.vo.KioskVO;
	import com.ettractions.kiosk.vo.LocationVO;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.robotlegs.mvcs.Actor;
	
	public class KioskDataService extends Actor
	{
		//private static const KIOSK_XML_PATH:String = "/Media/data/Kiosk.CDAT/xml/Kiosk_Jan25_v2.xml";
		private static const KIOSK_XML_PATH:String = "xml/Kiosk_Jan25_v2.xml";

		//private static const ASSETS_XML_PATH:String = "/Media/data/Kiosk.CDAT/xml/Asset_Jan25_v2.xml";
		private static const ASSETS_XML_PATH:String = "xml/Asset_Jan25_v2.xml";

		//private static const ASSET_PREFIX:String = "./data/Kiosk.CDAT";
		private static const ASSET_PREFIX:String = ".";
		
		private var _kioskData:XML;
		
		private var _loader:URLLoader;
		
		public function load():void
		{
			_loader = new URLLoader(new URLRequest(KIOSK_XML_PATH));			
			_loader.addEventListener(Event.COMPLETE, handleKioskDataLoaded);
		}
		
		private function handleKioskDataLoaded(event:Event):void
		{
			eventDispatcher.dispatchEvent(new KioskDataServiceEvent(KioskDataServiceEvent.KIOSK_DATA_FILE_LOADED));
			
			_kioskData = XML(_loader.data);
			
			_loader.removeEventListener(Event.COMPLETE, handleKioskDataLoaded);
			_loader.addEventListener(Event.COMPLETE, handleAssetsDataLoaded);
			_loader.load(new URLRequest(ASSETS_XML_PATH));
		}
		
		private function handleAssetsDataLoaded(event:Event):void
		{
			var categoryNodes:XMLList = _kioskData.Categories.PrimaryCategory;
			var brochureNodes:XMLList = XML(_loader.data).Brochures.Asset;
			var adNodes:XMLList = _kioskData.Kiosk.Ad.Advertisement;
			var categoryNode:XML;
			var kiosk:KioskVO = new KioskVO(
				_kioskData.Kiosk.Network.@Title,
				_kioskData.Kiosk.@HereIconCategory, 
				_kioskData.Kiosk.@AssetID,
				new LocationVO(null, null, null, null, Number(_kioskData.Kiosk.Location.@Latitude), Number(_kioskData.Kiosk.Location.@Longitude))
			);
			
			eventDispatcher.dispatchEvent(new KioskDataServiceEvent(KioskDataServiceEvent.ASSETS_DATA_FILE_LOADED));			
			
			_loader.removeEventListener(Event.COMPLETE, handleAssetsDataLoaded);
			_loader = null;
			
			for each (categoryNode in categoryNodes)
			{
				if (categoryNode.@ID == _kioskData.Kiosk.@HereIconCategory)
				{
					kiosk.categories.push(new CategoryVO(
						String(categoryNode.@ID),
						"Here"
					));								
				}
				else
				{
					kiosk.categories.push(new CategoryVO(
						String(categoryNode.@ID),
						String(categoryNode.@Name)
					));								
				}
			}
			
			for each (var adNode:XML in adNodes)
				kiosk.ads.push(new AdVO(
					String(adNode.@ID),
					ASSET_PREFIX + String(adNode.@ImagePath))
				);
			
			// TEMP
			for (var x:uint=0; x<21; x++)
				
			for each (var brochureNode:XML in brochureNodes)
			{
				var imageNodes:XMLList = brochureNode.Media.ContentImages;
				var brochure:BrochureVO = new BrochureVO(
					brochureNode.@ID,
					brochureNode.@Name,
					brochureNode.@SpecialTag,
					brochureNode.ProfileCallOut.@Text,
					brochureNode.FullSummary,
					brochureNode.Cost,
					new BrochureMediaVO(
						ASSET_PREFIX + brochureNode.Media.BrochureThumbnail.@ImagePath,
						ASSET_PREFIX + brochureNode.Media.MobileTag.@ImagePath,
						ASSET_PREFIX + brochureNode.Media.SpecialInfo.@ImagePath,
						brochureNode.Media.BookingInfo.@Phone,
						ASSET_PREFIX + brochureNode.Media.BookingInfo.@ImagePath,						
						brochureNode.Media.EmailInfo.@subject,
						brochureNode.Media.EmailInfo.@body,
						ASSET_PREFIX + brochureNode.Media.Video.@VideoPath
					),
					new ContactInformationVO(
						brochureNode.Phone,
						brochureNode.Fax,
						brochureNode.Website,
						brochureNode.Email
					),
					new LocationVO(
						brochureNode.Address,
						brochureNode.City,
						brochureNode.State,
						brochureNode.ZipCode,						
						Number(brochureNode.Map.Location.@Latitude),
						Number(brochureNode.Map.Location.@Longitude)
					)
				);
				
				categoryNodes = brochureNode.Categories.PrimaryCategory;
				
				for each (categoryNode in categoryNodes)
					for each (var category:CategoryVO in kiosk.categories)
						if (String(categoryNode.@ID) == category.id)
							brochure.categories.push(category);
				
				for each (var imageNode:XML in imageNodes)
					brochure.media.contentImagePaths.push(ASSET_PREFIX + imageNode.@ImagePath);
				
				kiosk.brochures.push(brochure);
			}
			
			// TEMP
			kiosk.brochures.push(brochure);
			
			eventDispatcher.dispatchEvent(new KioskDataServiceEvent(KioskDataServiceEvent.KIOSK_DATA_LOADED, kiosk));
		}
	}
}